﻿<%@ Page Language="C#" ValidateRequest="false" %>

<%@ Register Assembly="Typps" Namespace="Typps" TagPrefix="t" %>
<%--/*http://developer.yahoo.com/yui/yuitest/*/--%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

	protected void Page_Load(object sender, EventArgs e)
	{

	}

	protected void Editor1_SaveClick(object sender, TextSavingEventArgs e)
	{
		e.MessageToClient = "Your data has been saved.";
	}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
	<title></title>
	<!--CSS-->
	<link rel="stylesheet" type="text/css" href="../yuitest/css/logger.css" />
	<link rel="stylesheet" type="text/css" href="../yuitest/css/testlogger.css" />
	<!-- Dependencies -->
	<script src="../yuitest/yahoo-dom-event.js" type="text/javascript"></script>
	<script src="../yuitest/logger-min.js" type="text/javascript"></script>
	<!-- Source File -->
	<script src="../yuitest/yuitest-min.js" type="text/javascript"></script>
	<script src="../yuitest/event-simulate-min.js" type="text/javascript"></script>
</head>
<body id="body">
	<form id="form1" runat="server">
	<div>
		<asp:ScriptManager ID="ScriptManager1" ScriptMode="Debug" runat="server">
		</asp:ScriptManager>
		<t:Editor ID="Editor1" runat="server" IconsMode="ImageSprites" OnSaveClick="Editor1_SaveClick">
		</t:Editor>
	</div>
	</form>
	<script type="text/javascript">
		Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedHandler)
		function pageLoadedHandler(sender, eventArgs) {
			var oTestCase = new YAHOO.tool.TestCase({

				name: "TestCase Html editor",
				//---------------------------------------------
				// Setup and tear down
				//---------------------------------------------

				setUp: function () {
					this.editor = $find('<%= Editor1.ClientID %>');
				},

				tearDown: function () {
					this.editor.set_maxLength(0); //reset
					delete this.editor;
				},

				//    ---------------------------------------------
				//        Tests
				//    ---------------------------------------------
				test_add_href_tokenize1_ie: function () {
					var htmlText = '<a href="/Typps.Javascript.Tests/TestEditor.aspx">Home</a>';
					var output = this.editor.tokenizeUrls(Typps.Perform.Add, htmlText);
					var expected = '<a href="/Typps.Javascript.Tests/TestEditor.aspx" href2="/Typps.Javascript.Tests/TestEditor.aspx">Home</a>';
					if (this.editor._isIE)
						YAHOO.util.Assert.areEqual(expected, output, "IE specific test. Must append a href2 attribute of the original url");
				},
				test_remove_href_tokens_ie: function () {
					var htmlText = '<a href="/Typps.Javascript.Tests/TestEditor.aspx" href2="/Typps.Javascript.Tests/TestEditor.aspx">Home</a>';
					var output = this.editor.tokenizeUrls(Typps.Perform.Remove, htmlText);
					var removed = output.indexOf('href2="/Typps.Javascript.Tests/TestEditor.aspx">Home</a>') == -1;
					if (this.editor._isIE)
						YAHOO.util.Assert.areEqual(true, removed, "IE specific test. Must remove the custom href2 attribute from the element");
				},
				test_add_href_tokenize1_ie: function () {
					var htmlText = '<a href="#2">Home</a>';
					var output = this.editor.tokenizeUrls(Typps.Perform.Add, htmlText);
					var expected = '<a href="#2" href2="#2">Home</a>';
					if (this.editor._isIE)
						YAHOO.util.Assert.areEqual(expected, output, "IE specific test. Must append a href2 attribute of the original url");
				},
				test_remove_href_tokenize2_ie: function () {
					var htmlText = '<a href="#1" href2="#2">Home</a>';
					var output = this.editor.tokenizeUrls(Typps.Perform.Remove, htmlText);
					var removed = output.indexOf('href2="#2">Home</a>') == -1;
					if (this.editor._isIE)
						YAHOO.util.Assert.areEqual(true, removed, "IE specific test. Must remove the custom href2 attribute from the element");
				},
				testMaxLength: function () {
					var maxLengthReached = false;
					var totalCharactersCount = -1;
					this.editor.add_maxLengthReached(function (sender, args) {
						var maxLength = sender.get_maxLength();
						totalCharactersCount = args.get_commandArgument();
						maxLengthReached = true;
					});
					this.editor.set_maxLength(10);

					this.editor.set_htmlText('<p><span>The <strong>typps</strong> html editor recently implemented <i>max length.</i></p><p>And some more text here?</p>');
					var toolBars = this.editor.get_toolBars();
					var toolBarItem2 = toolBars.items[1]; //the Second toolbarset contains a save button
					var saveToolBarItem = toolBarItem2.item('save');
					YAHOO.util.UserAction.click(saveToolBarItem.get_element());
					YAHOO.util.Assert.areEqual(maxLengthReached, true, "Max length was reached. Something went wrong?");
					YAHOO.util.Assert.areEqual(totalCharactersCount, 78, String.format("Max length was reached and the total character count is : {0}", totalCharactersCount));
					this.editor.set_maxLength(79); //currently without html we have 78characters, this should pass
					maxLengthReached = false; //reset
					YAHOO.util.UserAction.click(saveToolBarItem.get_element());
					YAHOO.util.Assert.areEqual(maxLengthReached, false, "Max length was reached. Something went wrong?");
				},
				testSavingAndSavedHandlers: function () {
					var flag1 = false;
					var flag2 = false;
					this.editor.add_saving(function (sender, args) {
						flag1 = true;
					});
					this.editor.add_saved(function (sender, args) {
						flag2 = true;
					});
					var toolBars = this.editor.get_toolBars();
					var toolBarItem2 = toolBars.items[1]; //the Second toolbarset contains a save button
					var saveToolBarItem = toolBarItem2.item('save');
					YAHOO.util.UserAction.click(saveToolBarItem.get_element());
					YAHOO.util.Assert.areEqual(flag1, true, "Saving handler did not fire? noo it should have!");
					//Asyncronous test,,because saved fires after an ajax callback and it's not immediate
					this.wait(function () {
						YAHOO.util.Assert.areEqual(flag2, true, "Saved handler did not fire? noo it should have!");
					}, 2000);
				},
				testSaved: function () {
					var flag1 = false;
					var messageFromServer = null;
					this.editor.add_saved(function (sender, args) {
						flag1 = true;
						messageFromServer = args.get_commandArgument();
					});
					var toolBars = this.editor.get_toolBars();
					var toolBarItem2 = toolBars.items[1]; //the Second toolbarset contains a save button
					var saveToolBarItem = toolBarItem2.item('save');
					YAHOO.util.UserAction.click(saveToolBarItem.get_element());
					//Asyncronous test,,because saved fires after an ajax callback and it's not immediate
					this.wait(function () {
						YAHOO.util.Assert.areEqual(flag1, true, "Saved handler did not fire? noo it should have!");
						YAHOO.util.Assert.areEqual(messageFromServer, "Your data has been saved.", String.format("Message from server did not match {0}", messageFromServer));
					}, 2000);
				},
				testBrokenNestedUL: function () {
					var brokenHtml = "<ul><li>a<ul><li>b</li><ul></li><li>c</li></ul>";
					var expected = "<ul><li>a<ul><li>b</li><ul></ul></ul></li><li>c</li></ul>";
					this.editor.set_htmlText(brokenHtml);
					var result = this.editor.get_htmlText();
					YAHOO.util.Assert.areEqual(result, expected);
				}
			});

			//create the logger
			var logger = new YAHOO.tool.TestLogger();

			//add the test cases and suites
			YAHOO.tool.TestRunner.add(oTestCase);

			//run all tests
			YAHOO.tool.TestRunner.run();
		}
	</script>
</body>
</html>
